import axios from "axios";
import { LoginOut } from "@/common/LoginOut";
import { ElMessage } from "element-plus";

const instance = axios.create({
    baseURL: 'http://120.24.64.5:8088/mall-admin',
    timeout: 5000,
  });

  // 添加请求拦截器
instance.interceptors.request.use(function (config) {
  let token:string|null=localStorage.getItem('token')
  if(token){
    if(config.headers){
      config.headers['Authorization']=token
    }
  }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  let res=response.data
  if(res.code==401){
    // token过期
    LoginOut(()=>{
      // 退出登录接口请求
      console.log('退出接口调用');
    },()=>{
      // 退出登录后的处理
      ElMessage.warning('会话已过期,请重新登录!')
    });
  }
    return response.data;
  }, function (error) {
    return Promise.reject(error);
  });

export default instance;